#!/bin/bash
#
# clandestine.googlecode.com
#

shopt -s extglob

readonly PROCFNAME="/proc/acpi/toshiba/lcd"
readonly MIN=0
readonly CURRENT="`awk '/^brightness:/ {print $2}' "$PROCFNAME"`"
readonly MAX=$(( `awk '/^brightness_levels:/ {print $2}' "$PROCFNAME"` - 1 ))

function Set () {
	[[ "$1" ]] && echo "brightness:$1" > "$PROCFNAME"
}

[[ -r "$PROCFNAME" ]] || { echo "$PROCFNAME read test failed, aborting." > /dev/stderr; exit 1; }
case "$1" in
	"get"          ) echo "$CURRENT";;
	"off"          ) Set "$MIN";;
	"full"         ) Set "$MAX";;
	"more"         ) (( "$CURRENT" < "$MAX" )) && Set $(( $CURRENT + 1 ));;
	"less"         ) (( "$CURRENT" > "$MIN" )) && Set $(( $CURRENT - 1 ));;
	+([[:digit:]]) ) (( "$1" >= "$MIN" )) && (( "$1" <= "$MAX" )) && Set "$1";;
	*              ) echo "Syntax: `basename $0` { get | off | full | more | less | <level> }" > /dev/stderr;;
esac
